package com.lsmo.android.carfans.view.car;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.lsmo.android.carfans.R;
import com.lsmo.android.carfans.model.car.Brand;
import com.lsmo.android.carfans.model.forum.ForumThread;
import com.lsmo.android.carfans.process.forum.ForumProcess;
import com.lsmo.android.carfans.view.forum.ForunThreadListItemLayout;
import com.lsmo.android.common.base.BaseProcess;
import com.lsmo.android.common.base.BaseTask;
import com.lsmo.android.common.base.ViewProcessCallBack;

public class BrandListView extends LinearLayout implements ViewProcessCallBack {
	private ForumProcess brandListProcess;
	private ListView listView;
	ArrayList<Brand> BrandList;
	public String[] mStrings;
	public String strReString;
	public Context mContext ;
	private ViewGroup mProgressBarLayout;
	public View brandListItemLayouView;

	public BrandListView(Context context) {
		super(context);
		this.mContext = context;
		// TODO Auto-generated constructor stub
		this.brandListProcess = new ForumProcess();
		((Activity)context).getLayoutInflater().inflate(R.layout.select_view_only_list, this);
		this.postInvalidate();
		listView = (ListView)this.findViewById(R.id.listView);//listview
		mProgressBarLayout = (ViewGroup)this.findViewById(R.id.loading_layout);
	}

	
	
	@Override
	public void processAfterTaskFinish(int code, Object result ) {
		//选择品牌
		if(code == BrandListTask.GET_PRICE_EVERY_LIST){
			if(result!=null){
				listView.setAdapter(new BrandListAdapter(mContext, (List<ForumThread>)result)) ;
				listView.postInvalidate();
			}
		}
		this.mProgressBarLayout.setVisibility(View.GONE);
		this.listView.setVisibility(View.VISIBLE);
	}
	
	@Override
	public void updateView() {
		
		this.mProgressBarLayout.setVisibility(View.VISIBLE);
		this.listView.setVisibility(View.GONE);
		
			new BrandListTask(BrandListTask.GET_PRICE_EVERY_LIST,
					this.mContext,
					this,
					this.brandListProcess 	
		 ).execute(new String[]{});
	}
	
	
	private  class BrandListAdapter extends BaseAdapter{

		private  List<ForumThread> list ;
 		private Context mContext ;
		public BrandListAdapter(Context context, List<ForumThread> l) {
			// TODO Auto-generated constructor stub
			
			this.list = l ;
			this.mContext = context ;
		}
		
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			
			if(convertView==null){
				convertView = new ForunThreadListItemLayout(this.mContext); 
			}
			
			((ForunThreadListItemLayout)convertView).updateView(this.list.get(position));
			
			return convertView;
		}
	}
	
	private class BrandListTask extends BaseTask{
		public static final int GET_PRICE_EVERY_LIST = 4;//按品牌
		private ForumProcess brandListProcess ;
		
		public BrandListTask(int code , Context context,ViewProcessCallBack view,BaseProcess process ) {
			super(code, context, view);
			// TODO Auto-generated constructor stub
			this.brandListProcess = (ForumProcess) process;
		}
		
		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
 
		}
		
		@Override
		protected Object doInBackground(String... params) {
			// TODO Auto-generated method stub
			super.doInBackground(params);
			Object result = null;
	 
			switch(this.mCode){
				case GET_PRICE_EVERY_LIST:
				
					result = brandListProcess.getHotThreadsList(params);
					
				break;
			}
			return result;
		}
		
		@Override
		protected void onPostExecute(Object result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			
			switch(this.mCode){
			case GET_PRICE_EVERY_LIST:
			
			 	 mView.processAfterTaskFinish(this.mCode ,result);
				
			break;
		   }
	
		}
	}
}
